package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.dao.GreTransplantFileDao;
import cn.topevery.garden.dal.logic.GreTransplantLogic;
import cn.topevery.garden.entity.dto.GreTransplantDTO;
import cn.topevery.garden.entity.po.GreTransplant;
import cn.topevery.garden.entity.query.GreTransplantQuery;
import cn.topevery.garden.entity.vo.GreTransplantVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 占绿移植信息管理表
 *
 * @author code-generate
 * @date 2023-03-03
 */
@Api(tags = "占绿移植信息管理表")
@RestController
@RequestMapping("/greTransplant")
public class GreTransplantController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    GreTransplantLogic greTransplantLogic;

    @Resource
    GreTransplantFileDao greTransplantFileDao;


    /**
     * 获取占绿移植信息管理表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取占绿移植信息管理表页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<GreTransplantVO>> listPage(@RequestBody GreTransplantQuery query) {
        return JsonResult.ok(greTransplantLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 占绿移植信息管理表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody GreTransplantDTO dto) {
        greTransplantLogic.saveOrUpdate(getUser().id.toString(), dto);
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<GreTransplantVO> getInfoById(@RequestParam Integer id) {
        GreTransplant entity = greTransplantLogic.getEntity(id);
        GreTransplantVO vo = mapper.map(entity, GreTransplantVO.class);
        vo.setFileList(greTransplantFileDao.getListEntityByColumns(new ArrayList<KeyValue>(2) {{
            add(new KeyValue<>("c_db_status", 0));
            add(new KeyValue<>("c_transplant_id", id));
        }}));
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        greTransplantLogic.deleteBatchById(getUser().id.toString(), ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出占绿移植信息管理表信息")
    @GetMapping("/export")
    public void export(GreTransplantQuery query, HttpServletResponse response) {
        List<GreTransplantVO> list = greTransplantLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "占绿移植信息管理表", "sheet1", GreTransplantVO.class, "占绿移植信息管理表", response);
    }

    @PostMapping("/analysisExcel")
    @ApiOperation(value = "解析Excel")
    public JsonResult analysisExcelData(@RequestParam("file") MultipartFile file) {
        return greTransplantLogic.analysisExcel(file);
    }

    @PostMapping(value = "/importExcel")
    @ApiOperation(value = "导入Excel")
    public JsonResult importExcel(@RequestBody List<GreTransplantVO> greLandVOList) {
        return greTransplantLogic.importExcel(greLandVOList);
    }

}
